var $ = layui.jquery;
var laydate = layui.laydate;
var laytpl = layui.laytpl;
var layer = layui.layer;
var form = layui.form;
var id = 1;
$("button").click(openLog);

function openLog() {
var a;var b=null;
console.log(a==b,a===b)
	layer.open({
		type: 1,
		title: "林-林-林",
		content: $("#addd").html(),
		closeBtn: 2,
		shadeClose: true,
		skin: 'layui-layer-molv',
		area: ["460px", "440px"],
		success: function() {
			//渲染日期
			renderDate("birth");
			//给隐藏id++
			$("input[name='id']").val(id++)
			//渲染表单
			form.render();
			//add()方法给按钮添加监听
			add();
		}
	})
}


function renderDate(id) {
	laydate.render({
		elem: "#" + id,
		type: "date",
		value: new Date()
	})
}

function closeLayer() {
	layer.close(layer.index)
}

function add() {
	form.on('submit(add)', function(data) {

		var obj = data.field; //数据
		var hobby_val = '';
		hobby_val += obj.hobby1 ? obj.hobby1 + ' ' : '';
		hobby_val += obj.hobby2 ? obj.hobby2 + ' ' : '';
		hobby_val += obj.hobby3 ? obj.hobby3 + ' ' : '';
		obj.hobby_val = hobby_val;
		var temp = $("#tradd").html();
		var html = laytpl(temp).render(obj);
		$("#tbody").append(html);
		closeLayer();
	})
}

function testDelete(id) {
	layer.confirm("是否删除该数据？", function() {
		$("#tr" + id).remove();
		closeLayer();
	})
}

function testUpdate(id) {
	layer.open({
		type: 1,
		title: "林-林-林",
		content: $("#addd").html(),
		closeBtn: 2,
		shadeClose: true,
		skin: 'layui-layer-molv',
		area: ["460px", "500px"],
		success: function() {
			laydate.render({
				elem: '#birth',
				type: 'date'
			});
			var obj = {
				id: id,
				name: $("#tdb" + id).text(),
				birth: $("#tdc" + id).text(),
				sex: $("#tdd" + id).text(),
				addr: $("#tdf" + id).text()
			}
			console.log(obj)
			form.val("add", obj);
			$.each($("#tde" + id).text().split(" "), function(i, n) {
				$("input[type='checkbox'][value='" + n + "']").prop("checked", true)
			});
			form.render();
			save(id);
		}
	})
}

function save(id){
	form.on('submit(add)',function(data){
		var obj=data.field;
		var hobby_val='';
		 hobby_val+=obj.hobby1?obj.hobby1+" ":'';
		 hobby_val+=obj.hobby2?obj.hobby2+" ":'';
		 hobby_val+=obj.hobby3?obj.hobby3+" ":'';
		obj.hobby_val=hobby_val;
		$("#tdb"+id).html(obj.name);
		$("#tdc"+id).html(obj.birth);
		$("#tdd"+id).html(obj.sex);
		$("#tde"+id).html(obj.hobby_val);
		$("#tdf"+id).html(obj.addr);	
		closeLayer();
	})
}

function sel(){
	
	$("tbody tr").css("background",'');
	var i2=$("input[name='name2']").val();
	var i3=$("select[name='addr2']").val();
	console.log("~~~~"+i2)
	console.log("____"+i3)
	if(i2==''&&i3=='')return;
	$("tbody tr").each(function(){
		var name=$(this).find("td:eq(1)").html();
		var addr=$(this).find("td:eq(5)").html();
		var b1=false;
		var b2=false;
		if(i2==''||name.indexOf(i2)>-1)
		b1=true;
		if(i3==''||addr==i3)
		b2=true;
		if(b1&&b2){
			$(this).css("background","yellow");
		}
	})
}
